/*
 * Copyright (C) 2011 by ayoung <ayoung.p@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.peaceb.ruletherain;

import java.util.HashMap;
import java.util.Map;

/**
 * Weather information class
 */
public class WeatherInfo {
    public static final int NO_ID              = -1; /* There is no database id */
    
    public static final int UNKNOWN           = 0;
    public static final int CHANCE_OF_RAIN    = 1;
    public static final int CHANCE_OF_SNOW    = 2;
    public static final int CHANCE_OF_STORM   = 3;
    public static final int CHANCE_OF_TSTORM  = 4;
    public static final int CLOUDY            = 5;
    public static final int DUST              = 6;
    public static final int FLURRIES          = 7;
    public static final int FOG               = 8;
    public static final int HAZE              = 9;
    public static final int ICY               = 10;
    public static final int MIST              = 11;
    public static final int MOSTLY_CLOUDY     = 12;
    public static final int MOSTLY_SUNNY      = 13;
    public static final int PARTLY_CLOUDY     = 14;
    public static final int RAIN              = 15;
    public static final int SLEET             = 16;
    public static final int SMOKE             = 17;
    public static final int SNOW              = 18;
    public static final int STORM             = 19;
    public static final int SUNNY             = 20;
    public static final int THUNDERSTORM      = 21;
    
    public static final Map<String, Integer> weatherCodeMap;

    public  int     id;       /* database record id for user added item */
    public  double  latitude;
    public  double  longitude;
    public  double  altitude;
    public  int     weatherCode;
    public  String  cityName; /* city name will be used as query string also */
    public  String  condition;
    public  String  rawResult;
    public  boolean userAdd;
    
    static {
        weatherCodeMap = new HashMap<String, Integer>();
        weatherCodeMap.put("chance_of_rain",   Integer.valueOf(CHANCE_OF_RAIN  ));
        weatherCodeMap.put("chance_of_snow",   Integer.valueOf(CHANCE_OF_SNOW  ));
        weatherCodeMap.put("chance_of_storm",  Integer.valueOf(CHANCE_OF_STORM ));
        weatherCodeMap.put("chance_of_tstorm", Integer.valueOf(CHANCE_OF_TSTORM));
        weatherCodeMap.put("cloudy",           Integer.valueOf(CLOUDY          ));
        weatherCodeMap.put("dust",             Integer.valueOf(DUST            ));
        weatherCodeMap.put("flurries",         Integer.valueOf(FLURRIES        ));
        weatherCodeMap.put("fog",              Integer.valueOf(FOG             ));
        weatherCodeMap.put("haze",             Integer.valueOf(HAZE            ));
        weatherCodeMap.put("icy",              Integer.valueOf(ICY             ));
        weatherCodeMap.put("mist",             Integer.valueOf(MIST            ));
        weatherCodeMap.put("mostly_cloudy",    Integer.valueOf(MOSTLY_CLOUDY   ));
        weatherCodeMap.put("mostly_sunny",     Integer.valueOf(MOSTLY_SUNNY    ));
        weatherCodeMap.put("partly_cloudy",    Integer.valueOf(PARTLY_CLOUDY   ));
        weatherCodeMap.put("rain",             Integer.valueOf(RAIN            ));
        weatherCodeMap.put("sleet",            Integer.valueOf(SLEET           ));
        weatherCodeMap.put("smoke",            Integer.valueOf(SMOKE           ));
        weatherCodeMap.put("snow",             Integer.valueOf(SNOW            ));
        weatherCodeMap.put("storm",            Integer.valueOf(STORM           ));
        weatherCodeMap.put("sunny",            Integer.valueOf(SUNNY           ));
        weatherCodeMap.put("thunderstorm",     Integer.valueOf(THUNDERSTORM    ));
    }

    public WeatherInfo(String cityName, double latitude, double longitude, double altitude) {
        this.cityName  = cityName;
        this.latitude  = latitude;
        this.longitude = longitude;
        this.altitude  = altitude;
        this.userAdd   = false;
        this.id        = NO_ID;
    }

    public WeatherInfo(String cityName, double latitude, double longitude, double altitude, 
            boolean userAdd, int id) {
        this.cityName  = cityName;
        this.latitude  = latitude;
        this.longitude = longitude;
        this.altitude  = altitude;
        this.userAdd   = userAdd;
        this.id        = id;
    }
    
    public WeatherInfo(String cityName) {
        this.cityName  = cityName;
        this.id        = NO_ID;
    }
    
    public WeatherInfo() {
        this.id = NO_ID;
    }
    

    /**
     * 
     * @param code
     * @return
     */
    public static int getIconByWeatherCode(int code) {
        int resId;

        switch (code) {
            case CHANCE_OF_RAIN:
                resId = R.drawable.chance_of_rain;
                break;
            case CHANCE_OF_SNOW:
                resId = R.drawable.chance_of_snow;
                break;
            case CHANCE_OF_STORM:
                resId = R.drawable.chance_of_storm;
                break;
            case CHANCE_OF_TSTORM:
                resId = R.drawable.chance_of_tstorm;
                break;
            case CLOUDY:
                resId = R.drawable.cloudy;
                break;
            case DUST:
                resId = R.drawable.dust;
                break;
            case FLURRIES:
                resId = R.drawable.flurries;
                break;
            case FOG:
                resId = R.drawable.fog;
                break;
            case HAZE:
                resId = R.drawable.haze;
                break;
            case ICY:
                resId = R.drawable.icy;
                break;
            case MIST:
                resId = R.drawable.mist;
                break;
            case MOSTLY_CLOUDY:
                resId = R.drawable.mostly_cloudy;
                break;
            case MOSTLY_SUNNY:
                resId = R.drawable.mostly_sunny;
                break;
            case PARTLY_CLOUDY:
                resId = R.drawable.partly_cloudy;
                break;
            case RAIN:
                resId = R.drawable.rain;
                break;
            case SLEET:
                resId = R.drawable.sleet;
                break;
            case SMOKE:
                resId = R.drawable.smoke;
                break;
            case SNOW:
                resId = R.drawable.snow;
                break;
            case STORM:
                resId = R.drawable.storm;
                break;
            case SUNNY:
                resId = R.drawable.sunny;
                break;
            case THUNDERSTORM:
                resId = R.drawable.thunderstorm;
                break;
            case UNKNOWN:
            default:
                resId = R.drawable.unknown;
                break;
        }
        
        return resId;
    }
    
    /**
     * Determine is rainy day or not.
     * 
     * @return
     */
    public boolean isRain() {
        switch (weatherCode) {
            case RAIN:
            case STORM:
            case THUNDERSTORM:
            case SNOW:
            case MIST:
            case FLURRIES:
                
                // need to verify
//            case CHANCE_OF_RAIN:
//            case CHANCE_OF_SNOW:
//            case CHANCE_OF_STORM:
//            case CHANCE_OF_TSTORM:
                // TODO: need to check for Heavy Rain
                //        Paris position, light rain but not working
                // check Rain Showers
                // check Snow (not work)
                if (condition.equals("Drizzle") || condition.equals("Heavy Rain") ||
                    condition.equals("Rain Showers") ||
                    condition.equals("Blowing Snow")) {
                    /* Drizzle, Heavy Rain, Blowing Snow and Rain Showers are not working */
                    break;
                }
                return true;
                
            default:
                break;
        }
        return false;
    }
    
    /**
     * Get weather code by weather icon name from google weather API
     * 
     * @param filename
     * @return
     */
    public static int getWeatherCode(String filename) {
        Integer code = weatherCodeMap.get(filename);
        if (code != null) {
            return code.intValue();
        }
        else {
            return UNKNOWN;
        }
    }
    
    /**
     * Clear info
     */
    public void clear() {
        this.latitude    = 0;
        this.longitude   = 0;
        this.altitude    = 0;
        this.weatherCode = UNKNOWN;
        this.cityName    = null;
        this.condition   = null;
        this.rawResult   = null;
    }
}
